<?php

class OpcoesController extends Zend_Controller_Action 
{ 
	
	private $usuario;
	private $textosDAO;
	private $usuariosDAO;
	
	protected $_acl;
	protected $_session;

	
	
	public function init() {
		$config = Zend_Registry::get ( 'config' );
		$this->_acl = $this->getFrontController()->getParam('acl');
		$this->_session = new Zend_Session_Namespace ( $config->session->namespace );
		$this->usuario = $this->_session->storage;
		$this->textosDAO = new Textos();
		$this->usuariosDAO = new Usuarios();
		$this->view->aba = 'opcoes';	
		if ( isset ( $this->usuario )) {
			$this->view->perfil = $this->usuario->perfil;
		}
	}
	
	public function preDispatch() {
	 if (!Zend_Auth::getInstance()->hasIdentity()) {
      	$this->view->logado = 0;
      	$this->_redirect('/login');
      } else {
      	$config = Zend_Registry::get ( 'config' );
      	$this->_session = new Zend_Session_Namespace ( $config->session->namespace );
		$this->_usuario = $this->_session->storage;
       	$this->view->logado = 1;
      }
	}
	
	public function indexAction() {
		
		$params = new Zend_Filter_Input(null, null, $this->getRequest()->getParams());
		$usuario = $this->usuariosDAO->find($params->id)->current();
		
		$this->view->usuario = $this->usuario;
		
		
		
	}
	

	
	public function editar1Action() {
		$this->view->acao = 'editar1';
		$params = new Zend_Filter_Input(null, null, $this->getRequest()->getParams());
		
		$usuario = $this->usuariosDAO->find($params->id)->current();
		
		$this->view->usuario = $usuario;
		
		$this->render('formulario');
	}
	
	public function editar2Action() {
		$this->view->acao = 'editar2';
		$params = new Zend_Filter_Input(null, null, $this->getRequest()->getParams());
		
		$usuario = $this->usuariosDAO->find($params->id)->current();
		
		$this->view->usuario = $this->usuario;
		
		$this->render('formulario');
	}
	
	public function editarsenhaAction() {
		$this->view->acao = 'editarsenha';
		$params = new Zend_Filter_Input(null, null, $this->getRequest()->getParams());
		
		$usuario = $this->usuariosDAO->find($params->id)->current();
		
		$this->view->usuario = $this->usuario;
		
		$this->render('formulario');
	}
	
	
	public function salvarAction() {
		
		if (! $this->getRequest ()->isPost ()) {
			return $this->_forward ( 'index' );
		}
		
		$params = new Zend_Filter_Input(null, null, $this->getRequest()->getParams());

	
		
		$dados = array(
		'nome' => $params->nome,
		'login' => $params->login,
		'senha' => md5($params->senha),
		'sexo' => $params->sexo,
		'dataNascimento' => $params->dataNascimento,
		'rg' => $params->rg,
		'orgao' => $params->orgao,
		'instituicao' => $params->instituicao,
		'escolaridade' => $params->escolaridade,
		'profissao' => $params->profissao,
		'localTrabalho' => $params->localTrabalho,
		'perfil' => $params->perfil,
		'informacoesAdicionais' => $params->informacoesAdicionais,
		'email' => $params->email,
		'telefone' => $params->telefone,
		'endereco' => $params->endereco,
		'cep' => $params->cep,
		'bairro' => $params->bairro,
		'cidade' => $params->cidade,
		'estado' => $params->estado,	
		);
	
		$this->usuariosDAO->update($dados, "id = $params->id");
									
		$this->_forward("ok");
		
	}

	public function okAction() {
		$this->_helper->layout->disableLayout();
	}

	
	
}